package com.ruoyi.report_kf.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_kf.domain.bo.ReportKfTlsxzsjlbBaDetailBo;
import com.ruoyi.report_kf.domain.vo.ReportKfTlsxzsjlbBaDetailVo;
import com.ruoyi.report_kf.domain.ReportKfTlsxzsjlbBaDetail;
import com.ruoyi.report_kf.mapper.ReportKfTlsxzsjlbBaDetailMapper;
import com.ruoyi.report_kf.service.IReportKfTlsxzsjlbBaDetailService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 填料塑性指数试验检测记录详细Service业务层处理
 *
 * @author zyb
 * @date 2024-11-18
 */
@RequiredArgsConstructor
@Service
@DSTransactional
public class ReportKfTlsxzsjlbBaDetailServiceImpl implements IReportKfTlsxzsjlbBaDetailService {

    private final ReportKfTlsxzsjlbBaDetailMapper baseMapper;

    /**
     * 查询填料塑性指数试验检测记录详细
     */
    @Override
    public ReportKfTlsxzsjlbBaDetailVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询填料塑性指数试验检测记录详细列表
     */
    @Override
    public TableDataInfo<ReportKfTlsxzsjlbBaDetailVo> queryPageList(ReportKfTlsxzsjlbBaDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ReportKfTlsxzsjlbBaDetail> lqw = buildQueryWrapper(bo);
        Page<ReportKfTlsxzsjlbBaDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询填料塑性指数试验检测记录详细列表
     */
    @Override
    public List<ReportKfTlsxzsjlbBaDetailVo> queryList(ReportKfTlsxzsjlbBaDetailBo bo) {
        LambdaQueryWrapper<ReportKfTlsxzsjlbBaDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ReportKfTlsxzsjlbBaDetail> buildQueryWrapper(ReportKfTlsxzsjlbBaDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ReportKfTlsxzsjlbBaDetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getExcelId() != null, ReportKfTlsxzsjlbBaDetail::getExcelId, bo.getExcelId());
        lqw.eq(bo.getBaRowNumber() != null, ReportKfTlsxzsjlbBaDetail::getBaRowNumber, bo.getBaRowNumber());
        lqw.eq(bo.getHehId() != null, ReportKfTlsxzsjlbBaDetail::getHehId, bo.getHehId());
        lqw.eq(StringUtils.isNotBlank(bo.getJis()), ReportKfTlsxzsjlbBaDetail::getJis, bo.getJis());
        lqw.eq(StringUtils.isNotBlank(bo.getShiycs()), ReportKfTlsxzsjlbBaDetail::getShiycs, bo.getShiycs());
        lqw.eq(StringUtils.isNotBlank(bo.getHezl()), ReportKfTlsxzsjlbBaDetail::getHezl, bo.getHezl());
        lqw.eq(StringUtils.isNotBlank(bo.getHejssyzl()), ReportKfTlsxzsjlbBaDetail::getHejssyzl, bo.getHejssyzl());
        lqw.eq(StringUtils.isNotBlank(bo.getHejgsyzl()), ReportKfTlsxzsjlbBaDetail::getHejgsyzl, bo.getHejgsyzl());
        lqw.eq(StringUtils.isNotBlank(bo.getShuifzl()), ReportKfTlsxzsjlbBaDetail::getShuifzl, bo.getShuifzl());
        lqw.eq(StringUtils.isNotBlank(bo.getGantzl()), ReportKfTlsxzsjlbBaDetail::getGantzl, bo.getGantzl());
        lqw.eq(StringUtils.isNotBlank(bo.getHansl()), ReportKfTlsxzsjlbBaDetail::getHansl, bo.getHansl());
        lqw.eq(StringUtils.isNotBlank(bo.getSux()), ReportKfTlsxzsjlbBaDetail::getSux, bo.getSux());
        return lqw;
    }

    /**
     * 新增填料塑性指数试验检测记录详细
     */
    @Override
    public Boolean insertByBo(ReportKfTlsxzsjlbBaDetailBo bo) {
        ReportKfTlsxzsjlbBaDetail add = BeanUtil.toBean(bo, ReportKfTlsxzsjlbBaDetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改填料塑性指数试验检测记录详细
     */
    @Override
    public Boolean updateByBo(ReportKfTlsxzsjlbBaDetailBo bo) {
        ReportKfTlsxzsjlbBaDetail update = BeanUtil.toBean(bo, ReportKfTlsxzsjlbBaDetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ReportKfTlsxzsjlbBaDetail entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除填料塑性指数试验检测记录详细
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
